package com.example.friendcash.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.friendcash.BaseApplication;
import com.example.friendcash.R;
import com.example.friendcash.activities.PickFriendsActivity;
import com.example.friendcash.database.core.Friend;
import com.example.friendcash.database.services.interfaces.IFriendService;
import com.example.friendcash.database.services.interfaces.IServiceFactory;
import com.example.friendcash.library.viewfinder.IViewFinder;
import com.example.friendcash.library.viewfinder.ViewFinders;
import com.google.common.base.Predicate;

import java.util.List;

import static com.google.common.collect.Iterables.removeIf;

public class FriendsFragment extends BaseFragment {

    private ArrayAdapter<Friend> adapter;
    private IFriendService friendService;
    private Friend selectedFriendByContextMenu;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.friends_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final IServiceFactory serviceFactory = BaseApplication.getInstance().getServiceFactory();
        friendService = serviceFactory.createFriendService();
        BaseApplication.getInstance().friends.clear();
        BaseApplication.getInstance().friends.addAll(
                friendService.getAllFriends(
                        BaseApplication.getInstance().getEventId()));
        final IViewFinder viewFinder = ViewFinders.getViewFinder(getActivity());
        final ListView friendsListView = viewFinder.findListView(R.id.friends);
        adapter = new ArrayAdapter<Friend>(getActivity(), android.R.layout.simple_list_item_1, BaseApplication.getInstance().friends);
        friendsListView.setAdapter(adapter);
        registerForContextMenu(friendsListView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, view, menuInfo);
        final MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.event_list_view_context_menu, menu);

        final ListView listView = (ListView) view;
        final AdapterView.AdapterContextMenuInfo item = (AdapterView.AdapterContextMenuInfo) menuInfo;
        selectedFriendByContextMenu = (Friend) listView.getItemAtPosition(item.position);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (selectedFriendByContextMenu != null) {
            friendService.removeFriend(BaseApplication.getInstance().getEventId(), selectedFriendByContextMenu.id);
            removeIf(BaseApplication.getInstance().friends, new Predicate<Friend>() {
                @Override
                public boolean apply(Friend friend) {
                    return friend.id == selectedFriendByContextMenu.id;
                }
            });
            adapter.notifyDataSetChanged();
            return true;
        }
        return false;
    }

    @Override
    public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, com.actionbarsherlock.view.MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.friends_fragment_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        if (item.getItemId() == R.id.add_friend_button) {
            final Intent intent = new Intent(getActivity(), PickFriendsActivity.class);
            startActivityForResult(intent, 0);
        }
        return super.onOptionsItemSelected(item);
    }

    public void addFriendsAndUpdateView(List<Friend> friends) {
        for (Friend friend : friends) {
            final long addedFriendId = friendService.addFriend(BaseApplication.getInstance().getEventId(), friend);
            final Friend addedFriend = friendService.getEventById(addedFriendId);
            BaseApplication.getInstance().friends.add(addedFriend);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public String getTitle() {
        return "Friends";
    }

}
